package com.kongwg.java;

/**
 * @author kongwg
 * @create 2020-12-05 11:44
 */
class Windows2 extends Thread{
    private static int ticket=100;
    private static Object obj = new Object();
    @Override
    public void run() {
        while (true){
//            synchronized (obj) {
                synchronized(Windows2.class){

                //错的synchronized(this){
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName() + "卖出的票号为" + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}

public class WindowsTest2 {
    public static void main(String[] args) {
        Windows2 t1 = new Windows2();
        Windows2 t2 = new Windows2();
        Windows2 t3 = new Windows2();

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}

